package com.adakoda.android.asm;

import java.io.DataInput;
import java.io.EOFException;
import java.io.IOException;

public class ScreenShot {
  
    /** 
     * @param args 
     * @throws InterruptedException  
     */  
    public static void main(String[] args) throws InterruptedException {      
//        try {
            //分辨率大小，后续可以通过代码来获取到当前的分辨率  
//            int xResolution = 720;
//            int yResolution = 1280;
//            执行adb命令，把framebuffer中内容保存到fb1文件中
//             Runtime.getRuntime().exec("adb pull /dev/graphics/fb0 fb1");
//             等待几秒保证framebuffer中的数据都被保存下来，如果没有保存完成进行读取操作会有IO异常
//             Thread.sleep(10000);
             //读取文件中的数据  
//             InputStream in = (InputStream)new FileInputStream("fb1");
//             DataInput frameBuffer = new LittleEndianDataInputStream(in);
               
//             BufferedImage screenImage = new BufferedImage(
//                     xResolution, yResolution, BufferedImage.TYPE_INT_ARGB);
//                 int[] oneLine = new int[xResolution];
//                for (int y = 0; y < yResolution; y++) {
//                    //从frameBuffer中计算出rgb值
//                    convertToRgba32(frameBuffer, oneLine);
//                    //把rgb值设置到image对象中
//                    screenImage.setRGB(0, y, xResolution, 1, oneLine, 0, xResolution);
//                }
//                Closeables.closeQuietly(in);
//
//                ByteArrayOutputStream rawPngStream = new ByteArrayOutputStream();
//                FileOutputStream fout = new FileOutputStream("d:/1.png");
//                try {
//                      if (!ImageIO.write(screenImage, "png", fout)) {
//                        throw new RuntimeException(
//                            "This Java environment does not support converting to PNG.");
//                      }
//                    } catch (IOException exception) {
//                      // This should never happen because rawPngStream is an in-memory stream.
//                     System.out.println("IOException=" + exception);
//                    }
//                byte[] rawPngBytes = rawPngStream.toByteArray();
//                String base64Png = new Base64Encoder().encode(rawPngBytes);
//
//                File screenshot = OutputType.FILE.convertFromBase64Png(base64Png);
//                System.out.println("screenshot==" + screenshot.toString());
//                screenshot.renameTo(new File("C:\\screenshottemp.png"));
                  
//        } catch (IOException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//            System.out.println(e);
//        }
    }  
      
      
    public static void convertToRgba32(DataInput frameBuffer, int[] into) {  
        try {  
            for (int x = 0; x < into.length; x++) {  
                try{  
                int rgb = frameBuffer.readShort() & 0xffff;  
                int red = rgb >> 11;  
                red = (red << 3) | (red >> 2);  
                int green = (rgb >> 5) & 63;  
                green = (green << 2) | (green >> 4);  
                int blue = rgb & 31;  
                blue = (blue << 3) | (blue >> 2);  
                into[x] = 0xff000000 | (red << 16) | (green << 8) | blue;  
                }catch (EOFException e){  
                    System.out.println("EOFException=" + e);  
                }  
              }  
        } catch (IOException exception) {  
            System.out.println("convertToRgba32Exception=" + exception);  
      }  
    }  
      
}  